﻿using System.Net;
using System.Net.Mail;
using YerongapillyGreens.Web.Model;

namespace YerongapillyGreens.Web.Extensions
{
    public class Emailer
    {
        public bool SendEmail(EmailModel emailModel)
        {
            MailMessage mail = new MailMessage();
            mail.IsBodyHtml = true;
            mail.Subject = emailModel.Subject;
            mail.Body = emailModel.ToString();
            mail.From = new MailAddress(emailModel.EmailAddress, emailModel.Name);

            emailModel.ToAddress.ForEach(c => mail.To.Add(c));

            NetworkCredential nc = new NetworkCredential(Config.SMTPUsername, Config.SMTPPassword);

            using (SmtpClient client = new SmtpClient())
            {
                client.EnableSsl = true;
                client.UseDefaultCredentials = false;
                client.Credentials = nc;
                client.Host = Config.SMTPServer;
                client.Port = Config.SMTPServerPort.ToInt();
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                client.Send(mail);
            }

            return true;
        }
    }
}
